//
//  LoginViewController.m
//  witMorning
//
//  Created by 信晨汇 on 2018/1/8.
//  Copyright © 2018年 zt. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "ForgetPswViewController.h"
#import "AppDelegate.h"
#import "WMTabBarController.h"
@interface LoginViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)UITextField *lastTF;
@end

@implementation LoginViewController
{
    UITextField *accountTF;
    UITextField *pswTF;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.view.backgroundColor = [UIColor redColor];
    [self setUpView];
}


-(void)setUpView{
    
    UIImageView *backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_background.jpg"]];
    backImage.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H);
    backImage.userInteractionEnabled = YES;
    [self.view addSubview:backImage];
    
    UIImageView *iconImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"logo"]];
//    iconImage.frame = CGRectMake(SCREEN_W/2-50, 70, 100, 100);
    iconImage.centerX = backImage.centerX;
    iconImage.y = 50;
    [self.view addSubview:iconImage];
    
    
    CGRect rect = CGRectMake(70, 250, SCREEN_W-140, 40);
    accountTF = [self setupTextViewFrame:rect placeholder:@"工号" imageName:@"user_name"];
    accountTF.layer.borderWidth = 2;
    accountTF.layer.borderColor = [UIColor orangeColor].CGColor;
    [self.view addSubview:accountTF];
    self.lastTF = accountTF;
    
//    注册
    UIButton *registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(accountTF.right+5, accountTF.y+10, 40, 30)];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [registerBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
    registerBtn.tag = 10;
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(registerBtn.x, registerBtn.bottom-5, registerBtn.width, 1)];
    lineLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lineLabel];
    
    
    
    
    CGRect psw_rect = CGRectMake(70, accountTF.bottom+15, SCREEN_W-140, 40);
    pswTF = [self setupTextViewFrame:psw_rect placeholder:@"密码" imageName:@"password_login"];
    pswTF.secureTextEntry = YES;
    pswTF.layer.borderWidth = 2;
    pswTF.layer.borderColor = [UIColor clearColor].CGColor;
    [self.view addSubview:pswTF];
    
    //   忘记密码
    UIButton *psw_Btn = [[UIButton alloc]initWithFrame:CGRectMake(pswTF.right+5, pswTF.y+10, 40, 30)];
    [psw_Btn setTitle:@"忘记" forState:UIControlStateNormal];
    [psw_Btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    psw_Btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [psw_Btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:psw_Btn];
    psw_Btn.tag = 20;

    
    UILabel *lineLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(psw_Btn.x, psw_Btn.bottom-5, psw_Btn.width, 1)];
    lineLabel1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lineLabel1];
    
    
    UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(70, pswTF.bottom+50, pswTF.width, 40)];
    loginBtn.backgroundColor = [UIColor blueColor];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    loginBtn.layer.cornerRadius = 5;
    loginBtn.clipsToBounds = YES;
    [loginBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    loginBtn.tag = 30;
    
}


-(void)buttonClick:(UIButton *)sender{
    
    
    if (sender.tag == 10) {  //注册
        
        RegisterViewController *vc = [[RegisterViewController alloc]init];
        [self presentViewController:vc animated:YES completion:nil];
        
    }else if (sender.tag == 20){  //忘记
        
        ForgetPswViewController *vc = [[ForgetPswViewController alloc]init];
        [self presentViewController:vc animated:YES completion:nil];
        
    }else if (sender.tag == 30){  //登录
        
        if (accountTF.text.length == 0) {
            [MBProgressHUD showMessag:@"请输入工号" toView:self.view];
            return;
        }
        if(pswTF.text.length == 0){
            [MBProgressHUD showMessag:@"请输入密码" toView:self.view];
            return;
        }
        
        [self login];
  
    }
    
}

//创建textField
-(UITextField *)setupTextViewFrame:(CGRect)rect placeholder:(NSString *)placeholder imageName:(NSString *)imageName{
    
    
    UIView *View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    UIImageView *passView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
    [passView setImage:[UIImage imageNamed:imageName]];
    [View addSubview:passView];
    
    UITextField *accountTF = [[UITextField alloc]initWithFrame:rect];
    accountTF.delegate = self;
    accountTF.font = [UIFont systemFontOfSize:14];
    accountTF.layer.borderColor = [UIColor grayColor].CGColor;
    accountTF.layer.borderWidth = 0.5;
    accountTF.backgroundColor = [UIColor whiteColor];
    accountTF.placeholder = placeholder;
    accountTF.leftView = View;
    accountTF.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:accountTF];
    
    
    return accountTF;
    
    
}

//登录
-(void)login{
//    if (accountTF.text.length<1 || pswTF.text.length<1) {
//        return;
//    }else{
    
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UITextField class]]) {
            UITextField *field = (UITextField *)view;
            [field resignFirstResponder];
        }
    }
    
        NSString *strUrl = [BASE_HTTP stringByAppendingString:@"login"];
        [[WMManage shareInstance]postRequestWithUrl:strUrl andParam:@{@"user.usernumber":accountTF.text,@"user.password":pswTF.text} success:^(id responseObj) {
            NSLog(@"%@",responseObj);
            
            if ([responseObj[@"result"] isEqualToString:@"success"]) {
                ;
                USERDEF_SET_VALUE(responseObj[@"username"], @"username");
                USERDEF_SET_VALUE(accountTF.text, @"account");
                USERDEF_SET_VALUE(pswTF.text, @"password");
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                WMTabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:@"TabBarController"];
                
                AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                [delegate.window setRootViewController:tabBar];
            }else{
                [MBProgressHUD showMessag:@"登录失败" toView:self.view];
            }
            
        } fail:^(id error) {
            NSLog(@"%@",error);
            [MBProgressHUD showMessag:@"请求失败" toView:self.view];
            
        }];
//    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    self.lastTF.layer.borderColor = [UIColor clearColor].CGColor;
    textField.layer.borderColor = [UIColor orangeColor].CGColor;
    self.lastTF = textField;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
